package com.HRs.Dao;

import com.HRs.domain.Department;
import com.HRs.domain.Patient;
import com.HRs.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class aDDaolmpl implements aDDao{

    @Override
    public boolean Login_aD(String Id, String Password) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select ad_Password from administrators where ad_Account = '"+Id+"'";
            rs = st.executeQuery(sql);
            String password = "";
            while(rs.next()){
                password = rs.getString("ad_Password");
            }
            if(password.equals("")){
                return false;
            }else return password.equals(Password);

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            JDBCUtils.close(con, st, rs);
        }
    }

    @Override
    public void deleteDp_row(int row) {
        Integer r = row;
//        System.out.println("row : " + row);
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "call deleteDp_row(" +  r.toString() + ")";
            rs = st.executeQuery(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }
    }

    @Override
    public void insertDp(Department dp) {
        Connection con = null;
        Statement st = null;

        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();

            String sql = "insert into department_info (dp_Id, dp_Name, `dp_remarks`)\n" +
                    "values ('"+dp.getDp_Id()+"','"+dp.getDp_Name()+"','"+dp.getDp_remarks()+"');";

            st.executeUpdate(sql);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st);
        }
    }

    @Override
    public ArrayList<Department> get_Dp() {
        ArrayList<Department> adp = new ArrayList<>();

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select * from department_info";
            rs = st.executeQuery(sql);
            String dp_Id = "";
            String dp_Name = "";
            String dp_remarks = "";

            while(rs.next()){
                dp_Id = rs.getString("dp_Id");
                dp_Name = rs.getString("dp_Name");
                dp_remarks = rs.getString("dp_remarks");
                adp.add(new Department(dp_Id, dp_Name, dp_remarks));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }

        return adp;
    }

    @Override
    public ArrayList<String> get_DpName() {
        ArrayList<String> adp = new ArrayList<>();

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            con = JDBCUtils.getConnection();
            st = con.createStatement();
            String sql = "select * from department_info";
            rs = st.executeQuery(sql);
            String dp_Name = "";

            while(rs.next()){
                dp_Name = rs.getString("dp_Name");

                adp.add(new String(dp_Name));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con, st, rs);
        }

        return adp;
    }


}
